<?php
/**
 * 入职流程
 * ***********
 * 2013-02-17
 * 增加取消入职流程办理
 * *增加新阶段4，
 * *取消前，行政或IT已经办理，则可以执行取消办理操作
 * *同时处理入职流程批处理部分 DoList
 */
apf_require_controller("Abstract");
class Flow_AddStaffController extends AbstractController {

    private $error_item;//验证错误项
    public function handle_request() {
        /**
         * 验证角色
         */
        $params = $this->request->get_parameters();
        $params['submit'] = $params['Submit'];
        unset($params['Submit']);
        $entry_id = intval($params['id']);
        //是否需要回到addUser
        $this->_go_add_user($entry_id);
        //user
        $user = $this->_get_bll_flow_user()->get_user_by_id($entry_id);

        //角色 array('xz','it','hr'，qx_it，qx_xz);
        $role = array();//操作权限
        $operated = array();//已操作

        $role_list = $this->user_info['role_list'];
        //操作及查看权限
        $role_operate = $this->_check_now_operate_role($user,$role_list);

        //查看权限
        $role_operate = $this->_check_is_city_right($user,$role_operate,$role_list);
        if($role_operate == "error"){//没有查看权限
            $this->show_tips('ERROR_AUTH',"/flow/",Const_Tip::ERROR);
        }elseif(!empty($role_operate['role']) || !empty($role_operate['operated'])){
            $role = $role_operate['role'];
            $operated = $role_operate['operated'];
        }else{
            //只有查看权限
            $user = null;
        }
        //页面提交
        if($this->request->is_post_method()){
            $post_xz = $this->_check_post_xz($params,$role);
            $post_it = $this->_check_post_it($params,$role);
            $post_qx_it = $this->_check_post_qx_it($params,$role);
            $post_qx_xz = $this->_check_post_qx_xz($params,$role);
            if($post_xz && $post_it && $post_qx_xz && $post_qx_it){
                //处理页面提交提交
                $data = $this->_execute_submit($role,$params);
                $this->_get_bll_flow_user()->operate_entry($role, $params, $data,$this->get_user_id());
                //到待办事项列表页
                $this->show_tips('EDIT_ENTRY_SUCCESS',"/flow/?do=process",Const_Tip::SUCCESS);
            }else{
                 $user_info = $this->_get_user_info($user);
                 $user_info = $this->_build_post_error($user_info,$params);
            }
        }else{//查看
            if($user){
                //城市，部门职位，主管
                $user_info = $this->_get_user_info($user);
            }else{
                $this->response->redirect('/flow/');
            }
        }

        $this->request->set_attribute('user',$user_info);
        //角色
        $this->request->set_attribute('role_list',$role_list);
        $this->request->set_attribute('role',$role);
        $this->request->set_attribute('operated',$operated);
        //电脑类型
        $this->request->set_attribute('computer',Const_Flow::computer_type());
        //办公地点
        $address = Const_Flow::office_address();
        $this->request->set_attribute('work_space',$address[$user_info['work_city_id']]);
        //错误日志
        $this->request->set_attribute('error',$this->error_item);
        return 'Flow_AddStaff';
    }
    /**
     * 如果是当前阶段跳转到新增页面
     */
    private function _go_add_user($entry_id){
        $flow_data = $this->_get_flow_by_id($entry_id);
        if($flow_data['flow_opt'] == Const_Flow::PHASE_1){
            $log_list = $this->_get_bll_flow_log()->get_log($flow_data['flow_id'],$flow_data['flow_opt']);
            if(!empty($log_list) && $log_list['opt_user_id']==$this->user_info['user_id']){
                // TODO 多角色时有问题20130329
                // $this->response->redirect(Flow_Urls::add_user($entry_id));
            }
        }
    }
    /**
     * 检查是否有该待办事项的查看或编辑权限
     * @param unknown_type $role_operate
     */
    private function _check_is_city_right($user,$role_operate,$role_list){
        $right = false;
        //没有权限，跳出执行
        if(in_array($user['work_city_id'], $role_list[Const_Flow::ROLE_RECRUIT])){//招聘
             $right = true;
        }
        if(in_array($user['work_city_id'], $role_list[Const_Flow::ROLE_ADMIN])){//行政
            $right = true;
        }else{
            unset($role_operate['role']['xz']);unset($role_operate['operated']['xz']);
        }
        if(in_array($user['work_city_id'], $role_list[Const_Flow::ROLE_IT])){//it
            $right = true;
        }else{
            unset($role_operate['role']['it']);unset($role_operate['operated']['it']);
        }
        if(in_array($user['work_city_id'], $role_list[Const_Flow::ROLE_HR])){//hr
            $right = true;
        }else{
            unset($role_operate['role']['hr']);unset($role_operate['operated']['hr']);
        }
        if($right){
            return $role_operate;
        }else{
            return "error";
        }
    }
    /**
     * 返回当前可操作角色
     * //角色 array('xz','it','hr','zp');
     */
    private function _check_now_operate_role($user,$user_role){
        if(empty($user)){
            return false;
        }
        $role = array();
        $operated = array();//已经完成操作
        //流程结束
        $flow = $this->_get_flow_by_id($user['entry_id']);
        if($flow['now_status'] != Const_Flow::FLOW_STATUS_DOING){//****第三阶段或者已经生效****
            $operated['end'] = true;
            return array('role'=>$role,'operated'=>$operated); //已经结束,跳出执行
        }
        //已经取消入职,进入取消流程
        if($user['is_entry'] == Const_Flow::ENTRY_CANCEL && $user['user_id']){
            if(isset($user_role[Const_Flow::ROLE_IT])){
                $it_operated = $this->_get_bll_flow_log()->get_log_by_id_role($flow['flow_id'], Const_Flow::ROLE_IT, Const_Flow::PHASE_3);
                if($it_operated){
                    $role['qx_it'] = true; //取消流程,it
                }
            }
            if(isset($user_role[Const_Flow::ROLE_ADMIN])){
                $xz_operated = $this->_get_bll_flow_log()->get_log_by_id_role($flow['flow_id'], Const_Flow::ROLE_ADMIN, Const_Flow::PHASE_2);
                if($xz_operated){
                    $role['qx_xz'] = true; //取消流程,行政
                }
            }
            $operated['cancel'] = true;
            return array('role'=>$role,'operated'=>$operated); //已经结束,跳出执行
        }
        //行政招聘
        if(isset($user_role[Const_Flow::ROLE_RECRUIT])){
            $role['zp'] = true;
        }
        //行政管理
        if(isset($user_role[Const_Flow::ROLE_ADMIN])){
            if($user['is_entry'] == Const_Flow::ENTRY_OK && $user['user_id']){
            //提前确认入职，还没到第3阶段
                if($flow['flow_stage'] != Const_Flow::FLOW_ENTRY_3){//****提前确认入职，不更新阶段****
                    $role['xz'] = true;
                }else{
                    $operated['xz'] = true;
                }
            }else{//只要没有确认入职，均可编辑
                $role['xz'] = true;
            }
        }
        //IT
        if(isset($user_role[Const_Flow::ROLE_IT])){
            if($user['is_entry'] == Const_Flow::ENTRY_OK && $user['user_id']){
                //提前确认入职，还没到第3阶段
                if($flow['flow_stage'] != Const_Flow::FLOW_ENTRY_3){//****提前确认入职，不更新阶段****
                    $role['it'] = true;
                }else{
                    $operated['it'] = true;
                }
            }else{//只要没有确认入职，均可编辑
                $role['it'] = true;
            }
        }
        //HR
        if(isset($user_role[Const_Flow::ROLE_HR])){
            $role['hr'] = true;
            //已经确认入职
            if($user['is_entry'] == Const_Flow::ENTRY_OK && $user['user_id']){
                $operated['hr'] = true;
            }
        }
        return array('role'=>$role,'operated'=>$operated);
    }
    /**
     * 页面提交处理
     */
    private function _execute_submit($role,$params){
        $data = array();
        if(isset($role['xz']) && intval($params['submit']) == 3){
            $temp = $this->_build_post_xz($params);
            $data = array_merge($temp,$data);
            unset($role['hr']);//不可能再是hr提交
        }
        if(isset($role['it']) && intval($params['submit']) == 3){//IT提交
            $temp = $this->_build_post_it($params);
            $data = array_merge($temp,$data);
            unset($role['hr']);//不可能再是hr提交
        }
        if(isset($role['hr']) && intval($params['submit']) != 3){//HR提交
            $temp = $this->_build_post_hr($params);
            $data = $temp;
            unset($role['xz']);//不可能再是行政提交
            unset($role['it']);//不可能再是it提交
        }
        if(isset($role['qx_it']) && intval($params['submit']) == 3){//IT-取消-提交
            $temp = $this->_build_post_qx_it($params);
            $data = array_merge($temp,$data);
        }
        if(isset($role['qx_xz']) && intval($params['submit']) == 3){//行政-取消-提交
            $temp = $this->_build_post_qx_xz($params);
            $data = array_merge($temp,$data);
        }
        return $data;
    }
    /**
     * 取消入职流程 it提交
     */
    private function _build_post_qx_it($params){
        $result = array(
                'is_close_right'=>$params['is_close_right']?1:0,
        );
        return $result;
    }
    private function _check_post_qx_it($params,$role){
        $boolean = true;
        if(isset($role['qx_it']) && intval($params['submit']) == 3){//it提交
            $data = $this->_build_post_qx_it($params);
            if(!$data['is_close_right']){
                $this->error_item['is_close_right'] = '请确认关闭权限';
                $boolean = false;
            }
        }
        return $boolean;
    }
    /**
     * 取消入职流程 行政提交
     */
    private function _build_post_qx_xz($params){
        $result = array(
                'is_back_computer'=>$params['is_back_computer']?1:0,
                'is_back_phone'=>$params['is_back_phone']?1:0,
        );
        return $result;
    }
    private function _check_post_qx_xz($params,$role){
        $boolean = true;
        if(isset($role['qx_xz']) && intval($params['submit']) == 3){//行政提交
            $data = $this->_build_post_qx_xz($params);
            if(!$data['is_back_computer']){
                $this->error_item['is_back_computer'] = '请确认收回电脑';
                $boolean = false;
            }
        }
        return $boolean;
    }
    /**
     * 行政提交数据
     */
    private function _build_post_xz($params){
        $result = array(
                'computer'=>intval($params['computer']),
                'work_space'=>trim($params['work_space']),
                'seat'=>trim($params['seat']),
                'computer_code'=>trim($params['computer_code']),
                'office_phone'=>trim($params['office_phone']),
                'office_phone_ex'=>intval($params['office_phone_ex']),
                'is_instal'=>$params['is_instal']?1:0,
                );
        return $result;
    }
    /**
     * 检测行政提交数据
     */
    private function _check_post_xz($params,$role){
        $boolean = true;
        if(isset($role['xz']) && intval($params['submit']) == 3){//行政提交
            $data = $this->_build_post_xz($params);
            //必填项验证
            if(!$data['computer']){
                $this->error_item['computer'] = '请选择电脑';
                $boolean = false;
            }
            if($data['work_space']==''){
                $this->error_item['work_space'] = '请选择办公地点';
                $boolean = false;
            }
            if($data['seat']==''){
                $this->error_item['seat'] = '请输入座位';
                $boolean = false;
            }
            if($data['computer_code']==''){
                $this->error_item['computer_code'] = '请输入电脑编号';
                $boolean = false;
            }
        }
        return $boolean;
    }
    /**
     * IT提交数据
     */
    private function _build_post_it($params){
        $result = array(
                'domain_account'=>trim($params['domain_account']),
                //'crm_account'=>trim($params['crm_account']),
                'office_mail'=>trim($params['office_mail']),
        );
        return $result;
    }
    /**
     * IT提交数据验证
     */
    private function _check_post_it($params,$role){
        $boolean = true;
        if(isset($role['it']) && intval($params['submit']) == 3){//IT提交
            $data = $this->_build_post_it($params);
            //必填项验证
            if(!$data['domain_account']){//域帐号
                $this->error_item['domain_account'] = '请输入域帐号';
                $boolean = false;
            }
            /*if(!$data['crm_account']){//CRM帐号
                $this->error_item['crm_account'] = '请输入CRM帐号';
                $boolean = false;
            }*/
            if(!$data['office_mail'] || !preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $data['office_mail'])){//域帐号
                $this->error_item['office_mail'] = '请输入正确邮箱';
                $boolean = false;
            }
        }
        return $boolean;
    }
    /**
     * 还原提交状态
     */
    private function _build_post_error($user_info,$params){
        if(isset($params['is_instal'])){
            $user_info['is_instal'] = $params['is_instal'];
        }
        if(isset($params['computer'])){
            $user_info['computer'] = $params['computer'];
        }
        if(isset($params['work_space'])){
            $user_info['work_space'] = $params['work_space'];
        }
        if(isset($params['seat'])){
            $user_info['seat'] = $params['seat'];
        }
        if(isset($params['computer_code'])){
            $user_info['computer_code'] = $params['computer_code'];
        }
        if(isset($params['office_phone'])){
            $user_info['office_phone'] = $params['office_phone'];
        }
        if(isset($params['office_phone_ex'])){
            $user_info['office_phone_ex'] = $params['office_phone_ex'];
        }
        if(isset($params['re_entry_date'])){
            $user_info['re_entry_date'] = $params['re_entry_date'];
        }
        if(isset($params['domain_account']) && !isset($this->error_item['domain_account'])){
            $user_info['domain_account'] = $params['domain_account'];
        }
        if(isset($params['office_mail']) && !isset($this->error_item['office_mail'])){
            $user_info['office_mail'] = $params['office_mail'];
        }
        /*if(isset($params['crm_account']) && !isset($this->error_item['crm_account'])){
            $user_info['crm_account'] = $params['crm_account'];
        }*/
        return $user_info;
    }
    /**
     * HR提交数据
     */
    private function _build_post_hr($params){
        $result = array(
                're_entry_date'=>intval(date('Ymd',strtotime($params['re_entry_date']))),
                'is_entry'=>intval($params['submit'])
        );
        return $result;
    }

    /**
     * 获取用户详细信息
     * 城市
     * 部门职位
     * 主管
     */
    private function _get_user_info($user){
        //job
        $job_dep = $this->_get_bll_hroa()->get_job_dep_info($user['job_id']);
        $user['job_name'] = $job_dep['position_name'];
        $citys = $this->_get_bll_hroa()->get_city_list(false);
        $user['city_name'] = $citys[$user['work_city_id']]['city_name'];
        $user['department_name'] = $job_dep['bu_name'].$job_dep['department_name'];

        //leader
        $leader = $this->_get_bll_hroa()->get_user_base_info($user['parent_id']);
        $user['parent_name'] = $leader['real_name'];
        $user['parent_phone']= $leader['office_phone'];
        $user['parent_phone'] .= ($leader['office_phone']&&$leader['office_phone_ex'])?'-':'';
        $user['parent_phone'] .=$leader['office_phone_ex'];

        //date
        $user['re_entry_date'] = $user['re_entry_date']?date('Y-m-d',strtotime($user['re_entry_date'])):date('Y-m-d',strtotime($user['entry_date']));
        return $user;
    }
    /**
     * 获取待办事项
     */
    private function _get_flow_by_id($entry_id){
        if(!$this->flow){
            $this->flow = $this->_get_bll_flow()->get_flow_by_uid($entry_id);
        }
        return $this->flow;
    }
    /**
     * 入职bll
     * @return Bll_Flow_User
     */
    private function _get_bll_flow_user(){
        if(!$this->bll_flow_user ){
            apf_require_class('Bll_Flow_User');
            $this->bll_flow_user = new Bll_Flow_User();
        }
        return $this->bll_flow_user;
    }
    /**
     * hroa interface bll
     * @return Bll_Hr_Property
     */
    private function _get_bll_hroa(){
        if(!$this->bll_hroa ){
            apf_require_class('Bll_Hr_OutInterface');
            $this->bll_hroa = new Bll_Hr_OutInterface();
        }
        return $this->bll_hroa;
    }
    /**
     * 操作日志
     * @return Bll_Flow_Log
     */
    private function _get_bll_flow_log(){
        if(!$this->bll_flow_log ){
            apf_require_class('Bll_Flow_OperateLog');
            $this->bll_flow_log = Bll_Flow_OperateLog::get_instance();
        }
        return $this->bll_flow_log;
    }
    /**
     * 待办事项
     * @return Bll_Flow_Log
     */
    private function _get_bll_flow(){
        if(!$this->bll_flow ){
            apf_require_class('Bll_Flow_Info');
            $this->bll_flow = new Bll_Flow_Info();
        }
        return $this->bll_flow;
    }
}
